#
# fit the CO2 satellite data with a fixed lambda
# (but use a very small, unrealistic number of basis functions and levels so example
# runs quickly)
data(CO2)
# to look at raw data: quilt.plot(CO2$lon.lat, CO2$y, nx=288, ny=165)
# Use two levels starting at the second generation of lattice points from the triangulation
LKinfo0<- LKrigSetup( CO2$lon.lat, startingLevel=1 ,nlevel=2,
a.wght=1.1, alpha=c(1,.25),
LKGeometry="LKSphere" )
# Take a look at Model summary
print( LKinfo0)
# Use arbitrary lambda (.01)
obj0<- LKrig( CO2$lon.lat,CO2$y,LKinfo=LKinfo0, lambda=0.01)
if (FALSE) {
# Surface plot of estimate
surface(obj0, nx=288, ny=165)
world( add=TRUE)
}
if (FALSE) {
data(CO2)
# estimate lambda ( should be around 0.003)
# NOTE: lambda values will tend to be sensitive to the model choice
LKinfo0<- LKrigSetup( CO2$lon.lat, startingLevel=2 ,nlevel=2,
a.wght=1.1, alpha=c(1,.25),
LKGeometry="LKSphere")
obj0B<- LatticeKrig( CO2$lon.lat,CO2$y,LKinfo=LKinfo0)
surface( obj0B, col=terrain.colors(256))
world( add=TRUE, col="magenta")
# use chordal distance
LKinfo1<- LKrigSetup( CO2$lon.lat, startingLevel=2 ,nlevel=2,
a.wght=1.1, alpha=c(1,.25),
LKGeometry="LKSphere", distance.type="Chordal")
obj0C<- LatticeKrig( CO2$lon.lat,CO2$y,LKinfo=LKinfo1)
surface( obj0C, col=terrain.colors(256))
world( add=TRUE, col="magenta")
# a more serious model this uses about 3300 basis functions
LKinfo0<- LKrigSetup( CO2$lon.lat, startingLevel=3, ,nlevel=3,
a.wght=1.1, alpha=c(1, .5, .25),
LKGeometry="LKSphere" )
obj0B<- LatticeKrig( CO2$lon.lat,CO2$y,LKinfo=LKinfo0)
# takes about 1 minute on a Macbook air
# setting findAwght = TRUE takes about 8 minutes with
# lambda = 1.737 and a.wght = 15.8
}
#####################################
# The ring geometry
#####################################
if (FALSE) {
data(CO2)
LKinfo1<- LKrigSetup(CO2$lon.lat, NC=8 ,nlevel=1, lambda=.2,
a.wght=5, alpha=1,
LKGeometry="LKRing" )
obj1<- LatticeKrig( CO2$lon.lat,CO2$y,LKinfo=LKinfo1)
# take a look:
surface( obj1)
world( add=TRUE)
}
# compare to fitting without wrapping:
if (FALSE) {
LKinfo2<- LKrigSetup(CO2$lon.lat, NC=8 ,nlevel=1,
lambda=.2, a.wght=5, alpha=1 )
obj2<- LKrig( CO2$lon.lat,CO2$y,LKinfo=LKinfo2)
# NOTE: not periodic in longitude:
surface(obj2)
}
# a synthetic example and larger example
if (FALSE) {
set.seed(124)
N<- 1e4
x0<- matrix( rnorm(3*N), ncol=3)
x0<- x0/ sqrt( rowSums( x0^2))
x<- toSphere( x0 )
# the true function for testing -- a bump at the direction alpha
fun<- function(X){
alpha<- c( .1,.1,1)
alpha<- alpha/ sqrt( sum( alpha^2))
4*( 1 + c(( X)%*%alpha) )^2
}
ytrue <- fun(x0)
y<- ytrue + .05*rnorm( length(ytrue))
# this defines about 3300 basis functions
LKinfo1<- LKrigSetup( x,
startingLevel=3,
LKGeometry="LKSphere",
a.wght=1.01,
nlevel=3, alpha = c(1.0,.5,.25)^2,
choleskyMemory=list(nnzR= 20E6),
normalize=TRUE)
out<- LatticeKrig( x,y, LKinfo=LKinfo1, lambda=.01)
surface( out)
}
Run the code above in your browser using DataLab